/**
 * 文件：abstract_activity.java
 * 包名：com.ronri.ams.sample;   --- 正式发布时需要修正
 * 说明：页面控制基类
 * 作者：MS During
 * 日期：2017/5/9
 * 历史：
 */
package com.ronri.ams.sample;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.ronri.common.UrlEncode;
import com.ronri.iflytek.OnSpeechListening;
import com.ronri.iflytek.VoiceInput;
import com.ronri.mapapi.location.service.LocationManager;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.HashMap;
import java.util.Map;

public class abstract_activity
        extends AutoLayoutActivity
        implements OnActivityEvent
{

    // 显示标题
    private String mTitle = "";

    // 副标题（右边功能按钮）
    private String mvTitle = "";

    // 标题
    private TextView mtvTitle = null;

    // 副标题
    private TextView mtvvTitle = null;

    // 回退按钮
    private RelativeLayout mrlCancel= null;

    // OK返回
    private RelativeLayout mrlOk = null;

    /****
     * 设置标题
     * @param szTitle
     */
    public void setTitle(String szTitle, String szmvTitle){
        mTitle = szTitle;
        mvTitle = szmvTitle;
    }

    /***
     * 初始化环境
     */
    public void init(){

        // 获取窗体的标题信息
        mtvTitle = (TextView)findViewById(R.id.title_text_tv);
        mtvTitle.setText(mTitle);

        mtvvTitle = (TextView)findViewById(R.id.title_text_left);
        mtvvTitle.setText(mvTitle);

        // 绑定退回事件
        mrlCancel = (RelativeLayout)findViewById(R.id.btnBack);
        if(mrlCancel != null) {
            mrlCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (CancelReturn() == true){
                        finish();
                    }
                }
            });
        }

        // 绑定成功返回
        mrlOk = (RelativeLayout)findViewById(R.id.btnSure);
        if (mrlOk != null) {
            mrlOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(SureReturn()){
                        finish();
                    }
                }
            });
        }
    }

    /****
     * 窗体返回
     */
    public void activity_return(){
        finish();
    }

    // 放弃回退
    @Override
    public boolean CancelReturn() {
        toFinish();
        return true;
    }

    // 成功回退
    @Override
    public boolean SureReturn() {
        toFinish();
        return true;
    }

    /***
     * 组合JSON参数
     * @param nPageIndex
     * @param nPageSize
     * @param Paramerts
     * @return
     */
    public String MergeRemoteParamerts(int nPageIndex,
                                       int nPageSize,
                                       Map<String, Object> Paramerts)
    {
        Map<String, Object> oReturn = new HashMap<>();

        oReturn.put("page", nPageIndex);
        oReturn.put("pageSize", nPageSize);

        for(String key : Paramerts.keySet()){
            oReturn.put(key, Paramerts.get(key));
        }

        return UrlEncode.getUrlParamsByMap(oReturn, false);
    }

    /***
     * 回调结果
     * @param szString
     */
    public void CallBackResult(String szString) {

    }

    /***
     * 接收回调数据
     */
    private Handler messageHander = new Handler() {

        // 接收来自定位的消息
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            try {
                String szResult = msg.getData().getString("Result");
                CallBackResult(szResult);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    };


    /***
     * 获取语音信息
     * @return
     */
    public void getVoiceText(){
        VoiceInput.getVoiceText(this, new OnSpeechListening() {
            @Override
            public void OnSpeechResult(String szContent) {
                Message locMsg = messageHander.obtainMessage();

                Bundle bundle = new Bundle();
                bundle.putString("Result", szContent);
                locMsg.setData(bundle);
                messageHander.sendMessage(locMsg);
            }
        });
    }

    /****
     * 获取当前坐标位置的地址信息
     */
    public void getLoacteAddressText(){
        // 获取地址
        LocationManager.getLocationInfo(new LocationManager.OnLocationEvent(){
            @Override
            public void OnLocationResult(double lat, double lng, String szAddress) {
                Message locMsg = messageHander.obtainMessage();

                Bundle bundle = new Bundle();
                bundle.putString("Result", szAddress);
                locMsg.setData(bundle);
                messageHander.sendMessage(locMsg);
            }
        });
    }

    /***
     * 释放内存
     */
    public void toFinish(){
        finish();
        // setContentView(R.layout.s_free_layout);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        System.gc();
        System.runFinalization();
    }
}
